<?php
namespace App\Domain;
use App\Model\Subscribe as Model_Subscribe;
use App\Model\Live as Model_Live;

class Subscribe {

    /* 预约数量 */
	public function getSubscribeNums($uid) {

        $model = new Model_Subscribe();
        $rs= $model->getSubscribeNums($uid);
        
		return $rs;
	}

    /* 预约我的 */
	public function getTome($uid,$p) {

        $rs=[];
        
        $model = new Model_Subscribe();
        $model->delOverdue();
        $list= $model->getTome($uid,$p);
        
        foreach($list as $k=>$v){
            $userinfo=\App\getUserInfo($v['uid']);
            $usercoin=\App\getUserCoin($v['uid']);
            $data=[
                'id'=>$userinfo['id'],
                'subscribeid'=>$v['id'],
                'user_nickname'=>$userinfo['user_nickname'],
                'avatar'=>$userinfo['avatar'],
                'avatar_thumb'=>$userinfo['avatar_thumb'],
                'level'=>$userinfo['level'],
                'status'=>$v['status'],
				'type'=>$v['type'],
                'coin'=>number_format($usercoin['coin']),
            ];
            $rs[]=$data;
        }

		return $rs;
	}

    /* 我的预约 */
    public function getMeto($uid,$p) {
        
        $rs=[];

        $model = new Model_Subscribe();
        $model->delOverdue();
        $list= $model->getMeto($uid,$p);
        
        foreach($list as $k=>$v){
            $userinfo=\App\getUserInfo($v['liveuid']);
            $data=[
                'id'=>$userinfo['id'],
                'subscribeid'=>$v['id'],
                'user_nickname'=>$userinfo['user_nickname'],
                'avatar'=>$userinfo['avatar'],
                'avatar_thumb'=>$userinfo['avatar_thumb'],
                'level_anchor'=>$userinfo['level_anchor'],
                'status'=>$v['status'],
            ];
            $rs[]=$data;
        }

		return $rs;
	}

    /* 预约 */
    public function setSubscribe($data) {
        $rs = array('code' => 0, 'msg' => \PhalApi\T('预约成功'), 'info' => array());
        if(!$data){
            $rs['code'] = 1002;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        
        $liveuid=$data['liveuid'];
        $type=$data['type'];
        
        $model_live = new Model_Live();
        
        $liveuidinfo= $model_live->getLiveuidInfo($data['liveuid']);
        if(!$liveuidinfo){
            $rs['code'] = 1002;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        
        if($liveuidinfo['isauthor_auth']!=1){
            $rs['code'] = 1003;
			$rs['msg'] = \PhalApi\T('对方尚未认证成为主播');
			return $rs;
        }
        
        if($liveuidinfo['isdisturb']){
            $rs['code'] = 1004;
			$rs['msg'] = \PhalApi\T('主播已开启勿扰模式');
			return $rs;
        }
        
        if($type==1 && !$liveuidinfo['isvideo']){
            $rs['code'] = 1005;
			$rs['msg'] = \PhalApi\T('主播未开启视频接听');
			return $rs;
        }
        
        if($type==2 && !$liveuidinfo['isvoice']){
            $rs['code'] = 1006;
			$rs['msg'] = \PhalApi\T('主播未开启语音接听');
			return $rs;
        }
        
        
        $data['addtime']=time();

        $model = new Model_Subscribe();

        $result= $model->setSubscribe($data);
        
        if(!$result){
            $rs['code'] = 1003;
			$rs['msg'] = \PhalApi\T('预约失败，请重试');
			return $rs;
        }


		return $rs;
	}
	
}
